package net.itgoo.parkingpay.ui.orderinfo;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import net.itgoo.parkingpay.rest.ParkingVolleyManager;
import net.itgoo.parkingpay.rest.model.ParkingOrderModel;
import net.itgoo.parkingpay.vendor.mvp.ParkingMVPLoadDataCallback;

import org.json.JSONException;
import org.json.JSONObject;

public class ParkingOrderInfoDataRepository implements ParkingOrderInfoContract.DataSource {

    @Override
    public void getData(long orderId, final ParkingMVPLoadDataCallback callback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("orderid", orderId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParkingVolleyManager.sendJsonObjectRequest(Request.Method.POST,
                ParkingVolleyManager.baseUrl + "App_getOrderDetail.do", jsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            callback.onDataLoaded(ParkingOrderModel.parse(response));
                            return;
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        callback.onDataError("");
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        callback.onDataError(error.getMessage());
                    }
                });
    }

    @Override
    public void cancel() {

    }
}
